import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/8 9:46
 * @description 没想到预处理前缀和，求盘子的数量不需要再遍历一次
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] queries = {{1,17},{14,17}};
        solution.platesBetweenCandles("***|**|*****|**||**|*", queries);
    }
    public int[] platesBetweenCandles(String s, int[][] queries) {
        int n = s.length();
        // 前缀和求两个点之间的盘子数量
        int[] preSum = new int[n];
        for (int i = 0, sum = 0; i < n; i++) {
            if (s.charAt(i) == '*') {
                sum++;
            }
            preSum[i] = sum;
        }
        int[] left = new int[n];
        for (int i = 0, l = -1; i < n; i++) {
            if (s.charAt(i) == '|') {
                l = i;
            }
            left[i] = l;
        }
        int[] right = new int[n];
        for (int i = n - 1, r = -1; i >= 0; i--) {
            if (s.charAt(i) == '|') {
                r = i;
            }
            right[i] = r;
        }
        int[] ans = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int start = queries[i][0], end = queries[i][1];
            int x = right[start], y = left[end];
            ans[i] = x == -1 || y == -1 || x >= y ? 0 : preSum[y] - preSum[x];
        }
        return ans;
    }
}
